﻿using MyToolkit.Multimedia;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;
using Windows.Web.Syndication;
using Youtube_HD.Model;

namespace Youtube_HD.Services
{
    public class YoutubeServices : IYoutubeServices
    {
        IHttpChannel httpChannel;
        public YoutubeServices(IHttpChannel httpChannel)
        {
            this.httpChannel = httpChannel;
        }
        /// <summary>
        /// get Data Source and set source for MediaPlayer
        /// </summary>
        /// <param name="youTubeId"></param>
        /// <param name="maxQuality"></param>
        /// <param name="videoData"></param>
        /// <returns></returns>
        public async Task<YouTubeUri> GetVideoUriAsync(string youTubeId, YouTubeQuality maxQuality)
        {
            YouTubeUri urlVideo = null;
            try
            {
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    string videoId = youTubeId;
                    if (!videoId.Equals(String.Empty))
                    {
                        //Get The Video Uri and set it as a player source
                        var url = await YouTube.GetVideoUriAsync(videoId, YouTubeQuality.Quality480P);
                        if (url != null)
                            urlVideo = url;
                    }

                }
                else
                {
                    MessageDialog message = new MessageDialog("You're not connected to Internet!");
                    await message.ShowAsync();
                }
            }
            catch
            {
            }
            return urlVideo;
        }
        /// <summary>
        /// Get Youtube Channel
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task<List<YoutubeVideo>> GetYoutubeChannel(string url)
        {
            List<YoutubeVideo> youtubeVideo = new List<Model.YoutubeVideo>();
            try
            {
                var getData = await httpChannel.GetDataYoutubeAsync(url);
                if (!string.IsNullOrEmpty(getData))
                {
                    var data = ModelCreator<feed>.s_CreateInstanceFromXml(getData);
                    if (data.entry.Count() > 0)
                    {
                        foreach (var u in data.entry)
                        {
                            var duration = u.group.duration.seconds;
                            string a = u.link[0].href.ToString().Remove(0, 32);
                            var youtubeList = new YoutubeVideo()
                            {
                                Description = u.group.description.Value,
                                PubDate = "Published: " + u.published.ToString("mm-dd-yyyy"),
                                Thumbnail = "https://i.ytimg.com/vi/" + u.group.videoid + "/sddefault.jpg",
                                Title = u.title,
                                Id = a.Substring(0, 11),
                                Author = "Author: " + u.author.name,
                                ViewCount = "View: " + (u.statistics != null ? u.statistics.viewCount : 0),
                                Duration = "Duration: " + TimeSpan.FromSeconds(duration == 0 ? 0 : duration).Duration().ToString()
                            };

                            youtubeVideo.Add(youtubeList);
                        }
                    }
                }
            }
            catch
            {
                return null;
            }

            return youtubeVideo;
        }
        /// <summary>
        ///Get Youtube Playlist 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task<List<YoutubeVideo>> GetYoutubePlaylist(string url)
        {
            try
            {
               
                return null;
            }
            catch
            {
                return null;
            }
        }


        public async Task<List<YoutubeVideo>> GetYoutubeSearch(string url)
        {
            try
            {
                List<YoutubeVideo> youtubeVideo = new List<Model.YoutubeVideo>();
                var getData = await httpChannel.GetDataYoutubeAsync(url);
                if (!string.IsNullOrEmpty(getData))
                {
                    var data = ModelCreator<feed>.s_CreateInstanceFromXml(getData);
                    if (data.entry.Count() > 0)
                    {
                        foreach (var u in data.entry)
                        {
                            var duration = u.group.duration.seconds;
                            string a = u.link[0].href.ToString().Remove(0, 32);
                            var youtubeList = new YoutubeVideo()
                            {
                                Description = u.group.description.Value,
                                PubDate = "Published: " + u.published.ToString("mm-dd-yyyy"),
                                Thumbnail = "https://i.ytimg.com/vi/" + u.group.videoid + "/sddefault.jpg",
                                Title = u.title,
                                Id = a.Substring(0, 11),
                                Author = "Author: " + u.author.name,
                                ViewCount = "View: " + (u.statistics != null ? u.statistics.viewCount : 0),
                                Duration = "Duration: " + TimeSpan.FromSeconds(duration == 0 ? 0 : duration).Duration().ToString()
                            };

                            youtubeVideo.Add(youtubeList);
                        }
                    }
                }
                return youtubeVideo;
            }
            catch
            {
                return null;
            }
        }


        public async Task<List<YoutubeVideo>> GetYoutubeTrailler(string url)
        {
            List<YoutubeVideo> youtubeVideo = new List<Model.YoutubeVideo>();
            try
            {
                var getData = await httpChannel.GetDataYoutubeAsync(url);
                if (!string.IsNullOrEmpty(getData))
                {
                    var data = ModelCreator<feed>.s_CreateInstanceFromXml(getData);
                    if (data.entry.Count() > 0)
                    {
                        string nextPage=data.link[data.link.Count()-1].href;
                        foreach (var u in data.entry)
                        {
                            var duration = u.group.duration.seconds;
                            string a = u.link[0].href.ToString().Remove(0, 32);
                            var youtubeList = new YoutubeVideo()
                            {
                                Description = u.group.description.Value,
                                PubDate = "Published: " + u.published.ToString(),
                                Thumbnail = "https://i.ytimg.com/vi/" + u.group.videoid + "/default.jpg",
                                Title = u.title,
                                Id = a.Substring(0, 11),
                                Author = "Author: " + u.author.name,
                                ViewCount = "View: " + (u.statistics != null ? u.statistics.viewCount : 0),
                                Duration = "Duration: " + TimeSpan.FromSeconds(duration == 0 ? 0 : duration).Duration().ToString(),
                                NextPage = nextPage,
                            };

                            youtubeVideo.Add(youtubeList);
                        }
                    }
                }
            }
            catch
            {
                return null;
            }

            return youtubeVideo;
        }
    }
}
